package com.gongj.demo2;

import org.junit.Test;

import javax.swing.text.DateFormatter;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

import static java.time.temporal.TemporalAdjusters.lastInMonth;
import static java.time.temporal.TemporalAdjusters.nextOrSame;

public class TestLocalDateTime {

    @Test
    public void test3(){
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        //加年
        LocalDateTime localDateTime = now.plusYears(2)
                .plusMonths(2)
                .plusDays(3)
                .plusHours(3)
                .plusMinutes(10)
                .plusSeconds(5)
                .plusNanos(100);
        System.out.println(localDateTime);
//        System.out.println(now.getYear());
//        System.out.println(now.getMonthValue());
//        System.out.println(now.getDayOfMonth());
//        System.out.println(now.getHour());
//        System.out.println(now.getMinute());
//        System.out.println(now.getSecond());
//        System.out.println(now.getNano());
    }

    @Test
    public void test4() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        LocalDateTime localDateTime = now.minusYears(2)
                .minusMonths(2)
                .minusDays(3)
                .minusHours(3)
                .minusMinutes(10)
                .minusSeconds(5);
        System.out.println(localDateTime);
    }

    @Test
    public void test5() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now = " + now);
        LocalDateTime localDateTime = now.withYear(2020)
                .withMonth(5);
        System.out.println("localDateTime = " +localDateTime);
        //从1到28-31
        LocalDateTime localDateTime1 = now.withDayOfMonth(25);
        System.out.println("localDateTime1 = " +localDateTime1);
        //从1到365-366
        LocalDateTime localDateTime2 = now.withDayOfYear(90);
        System.out.println("localDateTime2 = " +localDateTime2);
        //从0到23
        LocalDateTime localDateTime3 = now.withHour(18);
        System.out.println("localDateTime3 = " +localDateTime3);
    }

    @Test
    public void test6(){
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        System.out.println("Month = " +now.getMonth());
        System.out.println("DayOfWeek = " +now.getDayOfWeek());
        System.out.println("Year = " + now.getYear());
        System.out.println("MonthValue = " +now.getMonthValue());
        System.out.println("DayOfMonth = " +now.getDayOfMonth());
        System.out.println("DayOfYear = " +now.getDayOfYear());
        System.out.println("Hour = " +now.getHour());
        System.out.println("Minute = " +now.getMinute());
        System.out.println("Second = " +now.getSecond());
        System.out.println("Nano = " +now.getNano());
    }
    @Test
    public void test7(){
        LocalDateTime time = LocalDateTime.parse("2021-01-03T16:45:33");
        LocalDateTime time1 = LocalDateTime.parse("2021-01-04T16:45:33");
        //此时间在指定时间之后，则为true
        System.out.println(time1.isAfter(time));
        //此时间在指定时间之前，则为true
        System.out.println(time1.isBefore(time));
    }

    @Test
    public void test8(){
        LocalDate time = LocalDate.parse("2021-01-03");
        LocalDate time1 = LocalDate.parse("2020-01-04");
        //检查此日期是否等于指定的日期
        System.out.println(time1.isEqual(time));
        //判断是否是闰年
        System.out.println(time1.isLeapYear());
    }
    // Instant 时间戳(以Unix元年 1970年1月1日 00:00:00到某个时间之间的毫秒值)
    @Test
    public void test9(){
        Instant now = Instant.now();//默认是UTC时区
        System.out.println(now);
        System.out.println(now.getEpochSecond()); // 秒
        System.out.println(now.toEpochMilli()); // 毫秒
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime);
    }
    @Test
    public void test10() {
        LocalDateTime from = LocalDateTime.of(2020, 12, 4, 9, 9, 9);
        LocalDateTime to = LocalDateTime.of(2021, 1, 4, 9, 9, 9);
        Duration between = Duration.between(from,to);

        // 这段时间的总天数
        System.out.println(between.toDays());
        // 这段时间的小时数
        System.out.println(between.toHours());
        // 这段时间的分钟数
        System.out.println(between.toMinutes());
        // 这段时间的秒数
        System.out.println(between.getSeconds());
        // 这段时间的毫秒数
        System.out.println(between.toMillis());
        // 这段时间的纳秒数
        System.out.println(between.getNano());
    }

    @Test
    public void test11() {
        LocalDate from = LocalDate.of(1998,4,2);
        LocalDate to = LocalDate.now();
        Period period = Period.between(from,to);
        System.out.println("年 = " + period.getYears());
        System.out.println("月 = " + period.getMonths());
        System.out.println("日 = " + period.getDays());
    }

    @Test
    public void test12() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
//        //获得下周日
//        LocalDateTime with = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
//        System.out.println(with);
//        //本月的第一天
//        LocalDateTime with2 = now.with(TemporalAdjusters.firstDayOfMonth());
//        System.out.println(with2);
//        //本月的最后一天
//        LocalDateTime with3 = now.with(TemporalAdjusters.lastDayOfMonth());
//        System.out.println(with3);
        
        // 自定义
        LocalDateTime localDateTime = now.with((t)->{
            LocalDateTime ldt = (LocalDateTime)t;
            //获得星期几
            DayOfWeek dayOfWeek = ldt.getDayOfWeek();
            //星期五
            if(dayOfWeek.equals(DayOfWeek.FRIDAY)){
                return ldt.plusDays(3);
            }else if (dayOfWeek.equals(DayOfWeek.SATURDAY)){
                //星期六
                return ldt.plusDays(2);
            }else {
                return ldt.plusDays(1);
            }
        });
        System.out.println(localDateTime);
    }

    @Test
    public void test13(){
        //将日期转换为字符串
        DateTimeFormatter ISO_DATE = DateTimeFormatter.ISO_DATE;
        LocalDateTime now = LocalDateTime.now();
        String format = ISO_DATE.format(now);
        System.out.println(format);
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
        String format1 = ofPattern.format(now);
        System.out.println(format1);

        //将字符串格式为日期时间
        String srtDate = "2021-01-03 21:27:02";
        DateTimeFormatter ofPattern2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(srtDate, ofPattern2);
        System.out.println(parse);
    }
    @Test
    public void test1(){
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);
    }

    @Test
    public void test2(){
        LocalDate localDate = LocalDate.of(2017, 10, 26);
        LocalTime localTime = LocalTime.of(15, 22, 56);
        LocalDateTime localDateTime = LocalDateTime.of(2020, 01,
                03, 15, 23, 55,963);
        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);
    }

}
